package com.lottery.im.module.result.adapter.result.statistics;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.lottery.im.R;
import com.lottery.im.api.result.entity.BallChooseInfo;
import java.util.List;

public class BallChooseAdapter extends BaseAdapter {
  private OnBallClickListener mOnBallClickListener;
  private List<BallChooseInfo> mDataList;
  private Context mContext;

  public BallChooseAdapter(List<BallChooseInfo> data, Context context) {
    mContext = context;
    mDataList = data;
  }

  @Override public int getCount() {
    return mDataList.size();
  }

  @Override public Object getItem(int position) {
    if (position >= mDataList.size() || position < 0) return null;
    return mDataList.get(position);
  }

  @Override public long getItemId(int position) {
    return position;
  }

  @Override public View getView(final int position, View v, ViewGroup parent) {
    final ViewHolder holder;
    if (v == null) {
      v = LayoutInflater.from(mContext).inflate(R.layout.item_ball_choose_list, parent, false);
      holder = new ViewHolder();
      holder.ball = v.findViewById(R.id.tv_ball);
      holder.layout = v.findViewById(R.id.layout);
      v.setTag(holder);
    } else {
      holder = (ViewHolder) v.getTag();
    }
    holder.ball.setText(mDataList.get(position).getBall());
    if (mDataList.get(position).isSelect()) {
      holder.ball.setTextColor(mContext.getResources().getColor(R.color.white));
      if ("0".equals(mDataList.get(position).getBall())) {
        holder.ball.setBackgroundResource(R.drawable.pk_ball_10);
      } else {
        holder.ball.setBackgroundResource(
            getResourceId("pk_ball_" + mDataList.get(position).getBall()));
      }
    } else {
      holder.ball.setTextColor(mContext.getResources().getColor(R.color.gray_99));
      holder.ball.setBackgroundResource(R.drawable.ball_choose_normal);
    }
    holder.layout.setOnClickListener(
        v1 -> mOnBallClickListener.onBallClick(position, mDataList.get(position).isSelect()));
    return v;
  }

  class ViewHolder {
    protected TextView ball;
    protected View layout;
  }

  //根据图片名称取得对应图片
  public int getResourceId(String name) {
    int resId = mContext.getResources().getIdentifier(name, "drawable", mContext.getPackageName());
    //解析资源文件夹下，id为resId的图片
    return resId;
  }

  public void setOnBallClickListener(
      OnBallClickListener onBallClickListener) {
    mOnBallClickListener = onBallClickListener;
  }

  public interface OnBallClickListener {
    void onBallClick(int position, boolean isSelect);
  }
}
